package com.corpgovernment.travelstandard.controller;

import com.corpgovernment.api.organization.dto.TravelStandrdSimpleAllReq;
import com.corpgovernment.api.travelstandard.bo.*;
import com.corpgovernment.api.travelstandard.service.IMbTravelstandCarService;
import com.corpgovernment.api.travelstandard.vo.*;
import com.corpgovernment.api.travelstandard.vo.car.CarTravelstandDto;
import com.corpgovernment.api.travelstandard.vo.response.IntlFlightShowResponse;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.travelstandard.entity.db.TravelStandardPost;
import com.corpgovernment.travelstandard.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/travelStandardPost")
public class TravelStandardPostController {

    @Autowired
    private TravelStandardPostService travelStandardPostService;

    @Autowired
    private MbTravelstandTrainService travelstandTrainService;

    @Autowired
    private AirportTransferStandardService airportTransferStandardService;

    @Autowired
    private TrainTransferStandardService trainTransferStandardService;

    @Autowired
    private MbTravelstandFlightService travelstandFlightService;

    @Autowired
    private MbTravelstandIntlFlightService mbTravelstandIntlFlightService;

    @Autowired
    private MbTravelstandHotelService travelstandHotelService;

    @Autowired
    private IMbTravelstandCarService mbTravelstandCarService;
    /**
     * 校验某岗位是否已关联差标
     * @param m
     * @return
     */
    @RequestMapping(value = "/checkTravelStandard")
    public JSONResult<TravelStandardPostVo> checkTravelStandard(@RequestBody TravelStandardPostVo m) {
        List<TravelStandardPost> travelStandardPosts = travelStandardPostService.list(m);
        TravelStandardPostVo travelStandardPostVo = new TravelStandardPostVo();
        if (travelStandardPosts != null && travelStandardPosts.size() > 0) {
            TravelStandardPost travelStandardPost = travelStandardPosts.get(0);
            travelStandardPostVo.setPostid(travelStandardPost.getPostid());
            travelStandardPostVo.setTsname(travelStandardPost.getTsname());
            travelStandardPostVo.setOrgid(travelStandardPost.getOrgid());
            travelStandardPostVo.setId(travelStandardPost.getId());
            travelStandardPostVo.setTstype(travelStandardPost.getTstype());

        }
        return JSONResult.success(travelStandardPostVo);
    }

    @RequestMapping(value = "/getSimpleTravelStandardAll")
    public JSONResult<TravelStandrdSimpleAllReq> getSimpleTravelStandardAll(@RequestBody TravelStandardAppRequestVo request){
        TravelStandrdSimpleAllReq result = new TravelStandrdSimpleAllReq();
        //----->>>>国内机票
        List<TravelStandrdSimpleAllReq.FlightTravelControl> flightTravelStandardList = new ArrayList<>();
        JSONResult<FlightTravelStandardShowVo> flightTravelStandardJson = travelstandFlightService.flightTravelStandardShow(request.getOrgId(), request.getUid(), request.getPostId(), null, null);
        FlightTravelStandardShowVo flightTravelStandard = flightTravelStandardJson.getData();
        flightTravelStandard.getPolicyRule().forEach(item ->{
            item.getPolicyDetail().forEach(i ->{
                TravelStandrdSimpleAllReq.FlightTravelControl flightTravelControl = new TravelStandrdSimpleAllReq.FlightTravelControl();
                flightTravelControl.setTitle(i.getTitle());
                flightTravelControl.setContext(i.getContent());
                flightTravelStandardList.add(flightTravelControl);
            });
        });
        result.setFlightTravelControlList(flightTravelStandardList);

        //----->>>>国际机票
        List<TravelStandrdSimpleAllReq.FlightInitTravelControl> flightInitTravelStandardList = new ArrayList<>();
        IntlFlightShowResponse intlFlightShowResponse = mbTravelstandIntlFlightService.intlFlightShow(request.getOrgId(), request.getUid(), request.getPostId(), request.getTrafficId(),request.getToken());
        intlFlightShowResponse.getPolicyDetail().forEach(i ->{
            TravelStandrdSimpleAllReq.FlightInitTravelControl flightInitTravelControl = new TravelStandrdSimpleAllReq.FlightInitTravelControl();
            flightInitTravelControl.setTitle(i.getTitle());
            flightInitTravelControl.setContext(i.getContent());
            flightInitTravelStandardList.add(flightInitTravelControl);
        });
        result.setFlightInitTravelControlList(flightInitTravelStandardList);

        //----->>>>火车
        List<TravelStandrdSimpleAllReq.TrainTravelControl> trainTravelStandardList = new ArrayList<>();
        JSONResult<TrainTravelStandardShowVo> trainTravelStandardJson = travelstandTrainService.trainTravelStandardShowApp(request.getOrgId(), request.getUid(), request.getPostId(), null,null);
        TrainTravelStandardShowVo trainTravelStandard = trainTravelStandardJson.getData();
        trainTravelStandard.getPolicyRule().stream().forEach(item ->{
            item.getPolicyDetail().forEach(i ->{

                if(Objects.equals(i.getTitle(), "超标管控方式")){
                    return;
                }

                TravelStandrdSimpleAllReq.TrainTravelControl trainTravelControl = new TravelStandrdSimpleAllReq.TrainTravelControl();
                trainTravelControl.setTitle(i.getTitle());
                trainTravelControl.setContext(i.getContent());
                trainTravelStandardList.add(trainTravelControl);
            });
        });
        result.setTrainTravelControlList(trainTravelStandardList);

        //----->>>>国内酒店
        List<TravelStandrdSimpleAllReq.HotelTravelControl> hotelTravelStandardList = new ArrayList<>();
        List<HotelForAppVo> hotelTravelStandards = travelstandHotelService.getHotelTravelForApp(request.getOrgId(), request.getPostId(), true);
        hotelTravelStandards.forEach(item ->{
            item.getRcDetail().forEach(i ->{
                if(Objects.equals(i.getTitle(), "超标管控方式")){
                    return;
                }
                TravelStandrdSimpleAllReq.HotelTravelControl hotelTravelControl = new TravelStandrdSimpleAllReq.HotelTravelControl();
                hotelTravelControl.setTitle(i.getTitle());
                hotelTravelControl.setContext(i.getContent());
                hotelTravelStandardList.add(hotelTravelControl);
            });
        });
        result.setHotelTravelControlList(hotelTravelStandardList);

        //----->>>>海外酒店
        List<TravelStandrdSimpleAllReq.HotelInitTravelControl> hotelInitTravelStandardList = new ArrayList<>();
        List<HotelForAppVo> hotelInitTravelStandards = travelstandHotelService.getHotelTravelForApp(request.getOrgId(), request.getPostId(), false);
        hotelInitTravelStandards.forEach(item ->{
            item.getRcDetail().forEach(i ->{
                if(Objects.equals(i.getTitle(), "超标管控方式")){
                    return;
                }
                TravelStandrdSimpleAllReq.HotelInitTravelControl hotelInitTravelControl = new TravelStandrdSimpleAllReq.HotelInitTravelControl();
                hotelInitTravelControl.setTitle(i.getTitle());
                hotelInitTravelControl.setContext(i.getContent());
                hotelInitTravelStandardList.add(hotelInitTravelControl);
            });
        });
        result.setHotelInitTravelControlList(hotelInitTravelStandardList);

        //----->>>>用车
        List<TravelStandrdSimpleAllReq.CarTravelControl> carStandardList = new ArrayList<>();
        CarTravelstandDto carTravelstandDto = new CarTravelstandDto();
        carTravelstandDto.setOrgId(request.getOrgId());
        carTravelstandDto.setPostId(request.getPostId() == null ? "" : request.getPostId()+"");
        CarStandardResponseBO carStandard = mbTravelstandCarService.getStandardText(carTravelstandDto);
        carStandard.getCarDescBOList().forEach(i ->{
            TravelStandrdSimpleAllReq.CarTravelControl carTravelControl = new TravelStandrdSimpleAllReq.CarTravelControl();
            carTravelControl.setTitle(i.getTitle());
            carTravelControl.setContext(i.getContent());
            carStandardList.add(carTravelControl);
        });
        result.setCarTravelControlList(carStandardList);

        //----->>>>接送机
        List<TravelStandrdSimpleAllReq.AirportTransferTravelControl> airportTransferStandardList = new ArrayList<>();
        AirportTransferStandardResponseBO airportTransferStandard = airportTransferStandardService.getTravelStandard(new AirportTransferStandardRequestBO(request.getOrgId(), request.getUid(), request.getPostId()));
        airportTransferStandard.getAirportTransferDescList().forEach(i ->{
            TravelStandrdSimpleAllReq.AirportTransferTravelControl airportTransferTravelControl = new TravelStandrdSimpleAllReq.AirportTransferTravelControl();
            airportTransferTravelControl.setTitle(i.getTitle());
            airportTransferTravelControl.setContext(i.getContent());
            airportTransferStandardList.add(airportTransferTravelControl);
        });
        result.setAirportTransferTravelControlList(airportTransferStandardList);

        //----->>>>接送站
        List<TravelStandrdSimpleAllReq.TrainTransferTravelControl> trainTransferStandardList = new ArrayList<>();
        TrainTransferStandardResponseBO trainTransferStandard = trainTransferStandardService.getTravelStandard(new TrainTransferStandardRequestBO(request.getOrgId(), request.getUid(), request.getPostId()));
        trainTransferStandard.getAirportTransferDescList().forEach(i ->{
            TravelStandrdSimpleAllReq.TrainTransferTravelControl trainTransferTravelControl = new TravelStandrdSimpleAllReq.TrainTransferTravelControl();
            trainTransferTravelControl.setTitle(i.getTitle());
            trainTransferTravelControl.setContext(i.getContent());
            trainTransferStandardList.add(trainTransferTravelControl);
        });
        result.setTrainTransferTravelControlList(trainTransferStandardList);
        return new JSONResult<>(result);
    }
}
